//
//  ChatListViewCell.m
//  GolfFriend
//
//  Created by Vescky on 14-2-17.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "ChatListViewCell.h"
#import "UIImageView+MJWebCache.h"
#import "UserSession.h"
#import "ChatDAO.h"
#import "ContactBook.h"
#import "DateUtility.h"
#import <QuartzCore/QuartzCore.h>

@interface ChatListViewCell () {
    NSMutableDictionary *chatListCellContent;
}
@end

@implementation ChatListViewCell

- (void)setDataInfo:(NSDictionary*)_dict {
    chatListCellContent = [[NSMutableDictionary alloc] initWithDictionary:_dict];
//    [imgvAvatar setImageURLStr:[chatListCellContent objectForKey:@"avatar"] placeholder:[UIImage imageNamed:@"comment_27.png"]];
    
    labelContent.text = [chatListCellContent objectForKey:@"content"];
    
    NSString *userId = [chatListCellContent objectForKey:@"fromId"];
    if ([userId isEqualToString:[[UserSession shareUserSession] getUserId]]) {
        userId = [chatListCellContent objectForKey:@"toId"];
    }
    if (isValueSet(userId)) {
        NSMutableDictionary *userPublicInfo = [[ContactBook shareContactBook] getUserInfo:userId];
        if (isValueSet([userPublicInfo objectForKey:@"nickName"])) {
            labelUserName.text = [userPublicInfo objectForKey:@"nickName"];
        }
        else {
            labelUserName.text = userId;
        }
        
        if (isValueSet([userPublicInfo objectForKey:@"time"])) {
            labelDate.text = [userPublicInfo objectForKey:@"time"];
        }
        
        if (isValueSet([userPublicInfo objectForKey:@"avatar"])) {
            [imgvAvatar setImageURLStr:[userPublicInfo objectForKey:@"avatar"] placeholder:Default_Avatar];
        }
    }
    imgvAvatar.layer.cornerRadius = imgvAvatar.frame.size.width / 2.0;
    
    int badgeNum = [[[ChatDAO shareChatDAO] getUnreadMessageByUserId:userId] count];
    if (badgeNum > 0) {
        labelBadge.text = [NSString stringWithFormat:@"%d",badgeNum];
        [viewBadge setHidden:NO];
    }
    
    labelDate.text = getWeiboDisplayTimeFromDate(getDateFromString(@"yyyy-MM-dd HH:mm:ss", [_dict objectForKey:@"time"]));
    
}

@end
